import {
  createApp,
  ref,
  h,
  resolveComponent
} from 'vue'

import installComponent from '@/plugin/installComponent'
import installPlugin from '@/plugin/installPlugin'

// 直接通过 resolveComponent 检索 需要使用的组件
// import { ConfigProvider } from 'ant-design-vue'
import zhCN from 'ant-design-vue/es/locale/zh_CN';


const createInstance = (App, options = {}) => {

  const instanceRef = ref('')   //  实例 引用

  const unmountInstance = () => {
    console.log('哈哈哈哈哈')
    app.unmount(dom)
    dom.parentNode.removeChild(dom)
    app = null
    console.log('un ffffffffffffffffffffffffffff')
    console.log(dom)
  }

  const props = Object.assign({ unmountInstance, ref: instanceRef }, options)

  let app = createApp({
    render() {
      return h(
        // ConfigProvider,
        resolveComponent('AConfigProvider'),        //  国际化语言组件
        {
          locale: zhCN
        },
        {
          default: () => h(App, props)
        }
      )
    }
  })

  const dom = document.createElement('div')
  document.body.appendChild(dom)

  app.use(installComponent).use(installPlugin).mount(dom)

  return {
    instanceRef,
    unmountInstance
  }

}

export default createInstance